<?php
class DefaultListAction extends FrontendAction
{
	public function __construct()
	{
		parent::__construct();
		$this->responseType = 'tpl';
		$this->layout = 'index';
		$this->data['menu']	= 'list';
	}

	public function execute()
	{
		$keyword	= $this->context->get('k');
		if(empty($keyword))
		{
			$keyword	= $this->context->post('k');
		}
		$keyword	= str_replace('/','',$keyword);
		$cat		= $this->context->get('c');
		if(empty($cat))
		{
			$cat	= $this->context->post('c');
		}
		$orderby	= $this->context->get('o');
		if(!in_array($orderby,array(1,2,3,4,5,6,7)))
		{
			$orderby	= 1;
		}
		$orderbys	= array(1=>'commissionNum_desc',2=>'commissionVolume_desc',3=>'credit_desc',4=>'delistTime_desc',5=>'commissionRate_desc',6=>'price_asc',7=>'price_desc');
		
		$pageSize			= 40;
		$lss				= new SettingService();
		$con				= $lss->get('list_setting',true);
		if(empty($con))
		{
			$con			= array();
		}
		else
		{
			if(array_key_exists('page_size',$con) && is_numeric($con['page_size']) && $con['page_size']>0 && $con['page_size']<=40)
			{
				$pageSize	= $con['page_size'];
			}
			unset($con['page_size']);
		}
		$area	= $this->context->get('d');
		if(!empty($area))
		{
			$con['area']	= $area;
		}
		$keyword = trim($keyword);
		if($keyword!='')
		{
			if(function_exists('mb_detect_encoding'))
			{
				$defaultEncode	= $this->data['config']['system']['encode'];
				if(!empty($defaultEncode) && $defaultEncode!='UTF-8')
				{
					$encode 	= mb_detect_encoding($keyword, C('CommonFunction')->getDefaultArray($defaultEncode,array('EUC-CN','ASCII','UTF-8','GB2312','GBK','BIG5')));
					if($encode!='UTF-8')
					{
						$keyword	= iconv($encode,'UTF-8',$keyword);
					}
				}
			}
			$keyword = trim($keyword);
			if(!empty($keyword))
			{
				$con['keyword']	= $keyword;
				$this->data['config']['search8']['keyword']	= $keyword;
			}
		}
		
		//检查关键词是否屏蔽
		if(!empty($keyword))
		{
			$settingService = C('SettingService');
			$forbiddenSetting = $settingService->get('forbidden_setting',true);
			if(!empty($forbiddenSetting['keywords']))
			{
				$keywords = explode("\n",str_replace("\r", "", $forbiddenSetting['keywords']));
				if(in_array($keyword,$keywords))
				{
					//屏蔽的商品列表
					$this->layout 	= 'result';
					$this->code		= 403;
					return;
				}
			}
		}
		
		if($cat>0)
		{
			$con['cid']		= $cat;
		}
		
		if(array_key_exists('cid',$con))
		{
			$cat			= $con['cid'];
		}
		else
		{
			
			$cat			= 0;
		}
		
		$categoryName		= '';
		$categoryService	= C('CategoryService');
		$categoryList		= $categoryService->getCategoryListByPId($cat);
		if($cat>0)
		{
			$parentCategoryList	= $categoryService->getParentList($cat);
			if($parentCategoryList)
			{
				$this->data['parentCategoryList']	= $parentCategoryList;
				$categoryName						= $parentCategoryList[count($parentCategoryList)-1]['name'];
				if($parentCategoryList[count($parentCategoryList)-1]['is_hide']==1)
				{
					$this->layout 	= 'result';
					$this->code		= 403;
					return;
				}
			}
		}
		
		$price		= $this->context->get('price');
		$prices		= array(1=>array('start_price'=>1,'end_price'=>50),2=>array('start_price'=>50,'end_price'=>100),3=>array('start_price'=>100,'end_price'=>200),4=>array('start_price'=>200,'end_price'=>500),5=>array('start_price'=>500,'end_price'=>1000),6=>array('start_price'=>1000,'end_price'=>2000),7=>array('start_price'=>2000,'end_price'=>5000),8=>array('start_price'=>5000,'end_price'=>500000),);
		if(is_numeric($price) && array_key_exists($price,$prices))
		{
			$con['start_price']			= $prices[$price]['start_price'];
			$con['end_price']			= $prices[$price]['end_price'];
		}
		$this->data['con']				= $con;
		$this->data['con']['orderby']	= $orderby;
		$this->data['con']['price']		= $price;
		
		$page			= $this->getPage();
		$ltConfig		= C('LtConfig');
		$pagerConfig	= $ltConfig->get('pagination.pager');
		
		$pagerConfig['per_page']		= $pageSize;
		if($this->data['isRobot'])
		{
			$pagerConfig['num_links_show']	= 100;
		}
		else
		{
			$pagerConfig['num_links_show']	= 15;
		}
		$ltConfig->addConfig(array('pagination.pager'=>$pagerConfig));
		
		$num 			= $pageSize;
		$start	 		= ($page-1) * $pageSize;
		$con['page_no']		= $page;
		$con['page_size']	= $pageSize;
		$con['sort']		= $orderbys[$orderby];
		
		$productService	= new ProductService();
		$productService->searchTaoKeProducts($con);
		$totalNum		= $productService->getTotalProcuct();
		$productList	= $productService->getProductList();
		$this->data['totalNum']		= $totalNum;
		$this->data['productList']	= $productList;
		
		$baseUrl 	= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>$orderby,'price'=>$price,'d'=>$area, 'p' => ':page'));
		
		$pagination 	= new PiscesPagination;
		$pagination->init();
		$this->data['pages'] = $pagination->pager($page, $totalNum, $baseUrl);
		
		$orderLink				= array();
		$orderLink['select']	= $orderby;
		$orderLink['num']		= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>1,'d'=>$area,'p' => $page));
		$orderLink['money']		= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>2,'d'=>$area,'p' => $page));
		$orderLink['credit']	= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>3,'d'=>$area,'p' => $page));
		$orderLink['publish']	= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>4,'d'=>$area,'p' => $page));
		$orderLink['rate']		= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>5,'d'=>$area,'p' => $page));
		$orderLink['priceasc']	= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>6,'d'=>$area,'p' => $page));
		$orderLink['pricedesc']	= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>7,'d'=>$area,'p' => $page));
		
		
		$this->data['orderby']	= $orderLink;
		
		$district	= C('LtConfig')->get('district');
		if(!empty($district))
		{
			$dtmp	= array();
			foreach($district as $dv)
			{
				$dtmp[]	= array('name'=>$dv,'link'=>C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$cat,'o'=>$orderby,'p' => $page,'d' => $dv)),'select'=>$area==$dv?'selected':'');
			}
			$this->data['district']	= $dtmp;
		}
		
		if($categoryList)
		{
			foreach($categoryList as &$clv)
			{
				$clv['link']	= C('LtUrl')->generate('Default', 'List', array('k'=>$keyword,'c'=>$clv['id'],'d'=>$area,'o'=>$orderby,'p' =>1));
			}
		}
		$this->data['categoryList']	= $categoryList;
		
		$hotkeyService				= C('HotkeyService');
		if($cat>0)
		{
			$hotkeyList					= $hotkeyService->getListByCategoryId($cat);
			$this->data['hotkeyList']	= $hotkeyList;
		}
		if($keyword!='' && $totalNum>0 && !$this->data['isRobot'])
		{
			if(empty($cat))
			{
				$cat	= 0;
			}
			$tmp	= $hotkeyService->getList(array('category_id'=>$cat,'name'=>$keyword),'',0,1);
			if(empty($tmp['rows']))
			{
				$hotkeyService->add(array('category_id'=>$cat,'name'=>$keyword,'orderby'=>0));
			}
		}
		
		if(!$this->data['isRobot'])
		{
			$adService		= new AdService();
			$adList			= $adService->getAdList(true,array('position'));
			$this->data['adList']	= $adList;
		}
		
		if($page==1)
		{
			$page	= '';
		}
		
		$seoConfig		= C('SettingService')->get('seo_setting',true);
		$seoTitle		= $this->getSeoStr($seoConfig['listtitle'],array('keyword'=>$keyword,'catName'=>$categoryName,'page'=>$page));
		$seoKeyword		= $this->getSeoStr($seoConfig['listkeyword'],array('keyword'=>$keyword,'catName'=>$categoryName,'page'=>$page));
		$seoDescription	= $this->getSeoStr($seoConfig['listdescription'],array('keyword'=>$keyword,'catName'=>$categoryName,'page'=>$page));
		
		$this->data['seo']['title']			= $seoTitle;
		$this->data['seo']['keyword']		= $seoKeyword;
		$this->data['seo']['description']	= $seoDescription;
	}
}
